﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;

namespace GravityDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Timer tickTimer;
        Planet Sun;
        Planet Earth;
        
        public MainWindow()
        {
            InitializeComponent();

            Sun = new Planet();
            Earth = new Planet();

            Sun.Mass = 1000000000000;
            Sun.Radius = 100;
            Sun.XPosition = 400;
            Sun.yPosition = 400;

            Earth.Mass = 3;
            Earth.Radius = 5;
            Earth.XPosition = 100;
            Earth.YPosition = 100;
            Earth.XVelocity = -10;
            Earth.YVelocity = 10;

            tickTimer = new Timer();
            tickTimer.AutoReset = true;
            tickTimer.Interval = 1000;
            tickTimer.Elapsed += new ElapsedEventHandler(tickTimer_Elapsed);

            tickTimer.Enabled = true;
        }

        void tickTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                Dispatcher.BeginInvoke((Action)(() => tickTimer_Elapsed(sender, e)));
            }
            else
            {
                #region Physics Tick

                double distance = Math.Pow(Sun.XPosition - Earth.XPosition, 2) + Math.Pow(Sun.YPosition - Earth.yPosition, 2);

                double force = .5 * Sun.Mass * Earth.Mass / (Math.Pow(distance, 2));
                double angle = Math.Atan((Sun.XPosition - Earth.XPosition) / (Sun.YPosition - Earth.yPosition));

                Earth.YVelocity += Math.Cos(angle) * (force / Earth.Mass);
                Earth.XVelocity += Math.Sin(angle) * (force / Earth.Mass);

                Earth.XPosition += Earth.XVelocity;
                Earth.YPosition += Earth.YVelocity;

                #endregion

                #region Draw

                Canvas MainCanvas = new Canvas();
                this.Content = MainCanvas;
                MainCanvas.Background = Brushes.Black;

                Image EarthImage = Earth.BodyImage;
                Image SunImage = Sun.BodyImage;

                MainCanvas.Children.Add(EarthImage);
                MainCanvas.Children.Add(SunImage);

                Canvas.SetLeft(EarthImage, Earth.XPosition - Earth.Radius);
                Canvas.SetTop(EarthImage, Earth.YPosition - Earth.Radius);

                Canvas.SetLeft(SunImage, Sun.XPosition - Sun.Radius);
                Canvas.SetTop(SunImage, Sun.YPosition - Sun.Radius);

                #endregion
            }
        }
    }
}
